package other.edan.pass;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rkhd.platform.sdk.ScriptTrigger;
import com.rkhd.platform.sdk.data.model.Account;
import com.rkhd.platform.sdk.data.model.Opportunity;
import com.rkhd.platform.sdk.data.model.Product;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.DataModel;
import com.rkhd.platform.sdk.model.OperateResult;
import com.rkhd.platform.sdk.param.ScriptTriggerParam;
import com.rkhd.platform.sdk.param.ScriptTriggerResult;
import other.edan.pass.utils.ComUtils;
import other.edan.pass.utils.DateTools;
import other.edan.pass.utils.HCXObjectService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**功能描述：新增客户后，更新CRM编码和K3编码
 * @ClassName: AccountAfterAddTri
 * @Description:1.DM{6位}
 * @author 范伟绵
 * @date 2024年08月30日
 */

public class AccountAfterAddTri implements ScriptTrigger {
	private static final Logger LOGGER = LoggerFactory.getLogger();
	private static final String LOG_MSG_EX="新增客户后，更新CRM编码：";
	
	@Override
	public ScriptTriggerResult execute(ScriptTriggerParam arg0) throws ScriptBusinessException {
		LOGGER.debug(LOG_MSG_EX+"开始");
		JSONObject accountJson = null;
		try {
			List<DataModel> dmList = arg0.getDataModelList();
			for (DataModel dm: dmList) {
				accountJson =JSONObject.parseObject(dm.toString());
				LOGGER.info("客户信息:" + accountJson.toJSONString());
			}

			if (accountJson.getLong("entityType").equals(3314472397658728L)){
				//渠道客户
				rewriteCrmCode(accountJson.getLong("id"));
			}

			return new ScriptTriggerResult(dmList);
		} catch (Exception e) {
			LOGGER.info(e.getMessage());
			throw new ScriptBusinessException("新增客户后，更新CRM编码：" + e.getMessage());
		}
	}

	private  void  rewriteCrmCode(Long id) throws IOException {
		String sql="select id,crmCodeOnly__c from account WHERE crmCodeOnly__c>0  order by crmCodeOnly__c desc";
		Account bigAccount=HCXObjectService.queryOne(sql);
		String code="1.DM"+String.format("%06d",bigAccount.getCrmCodeOnly__c().longValue()+1);

		Account account=new Account();
		account.setId(id);
		account=HCXObjectService.get(account);

		Account updateAccount=new Account();
		updateAccount.setId(id);
		updateAccount.setCrmCode__c(code);
		if(null==account.getK3Code__c() || account.getK3Code__c().isEmpty()){
			updateAccount.setK3Code__c(code);
		}

		LOGGER.info(updateAccount.toString());

		HCXObjectService.update(updateAccount);
	}

	public static void main(String[] args) throws IOException {
		//AccountAfterAddTri api=new AccountAfterAddTri();
		//api.rewriteCrmCode(3391878381607527L);

		/*String sql="select id,fTaxNum__c,unifiedSocialCreditIdentifier__c,accountName from account " +
				"where (unifiedSocialCreditIdentifier__c is  null) and (fTaxNum__c='91210112702087542R')" +
				" and  (entityType=3314472397658728)";
		List<Account> accountList=HCXObjectService.queryAll(sql);
		System.err.println(accountList.size());
		for(Account account:accountList){
			System.err.println(account.getAccountName());
			account.setUnifiedSocialCreditIdentifier__c(account.getFTaxNum__c());
			System.err.println(HCXObjectService.update(account).getErrorMessage());
			//break;
		}*/

		/*String sql="select id from product where approvalStatus!=3";
		List<Product> productList=HCXObjectService.queryAll(sql);
		System.err.println(productList.size());
		for(Product product:productList){
			Product updateProduct=new Product();
			updateProduct.setId(product.getId());
			updateProduct.setApprovalStatus(3);
			System.err.println(HCXObjectService.update(updateProduct).getErrorMessage());
		}*/


	}
}
